﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Leiktuther.ViewModels;


namespace Leiktuther.Models
{
    public class GameRepository
    {
        private IAppRepository _repository = null;

        public GameRepository(IAppRepository rep) 
        { 
            _repository = rep;
        }

        public GameRepository()
        {
            _repository = new AppRepository();
        }
        
        public IEnumerable<Game> getGames()
        {
            var entities = _repository.Games;
            return entities;
        }

        public IEnumerable<Player> getWins(int score)
        {
            var model = from pl in _repository.Players
                        join st in _repository.Statistics on pl.ID equals st.PlayerID
                        where st.TotalGames > score
                        select pl;
            return model;
        }
        /*
                public IEnumerable<HighScoreModel> getHighScoreByGameId(int Id)
                {
                    var game = getGameById(Id);
                    var model = from ga in _repository.Games
                                join st in _repository.Statistics on 
                                from AllOfIt in allOfIt.DefaultIfEmpty(
                
                                join st in _repository.Statistics on ga.ID equals st.GameID
                                join pl in _repository.Players on st.PlayerID equals pl.ID
                        
  
                        
                    return null;
        
                }
                */
        public Game getGameByRefer(string what)
        {
            Game model = (from ga in _repository.Games
                          where ga.GameRefer == what
                          select ga).SingleOrDefault();
            return model;
        }

        public Game getGameById(int Id)
        {
            var model = (from ga in _repository.Games
                         where ga.ID == Id
                         select ga).SingleOrDefault();
            return model;
        }

        public GameInstance getInstanceById(int Id)
        {
            var model = (from inst in _repository.Instances
                        where inst.ID == Id
                        select inst).SingleOrDefault();
            return model;
        }

        public IEnumerable<GameInstance> getInstancesForGameId(int Id)
        {
            var model = (from inst in _repository.Instances
                         where inst.GameID == 1 && inst.isActivated == true
                         select inst);
            return model;
        }

        public IEnumerable<StatisticsViewModel> getPlayerStatistics()
        {
            var model = (from st in _repository.Statistics
                         orderby st.Win descending
                         join pl in _repository.Players on st.PlayerID equals pl.ID
                         join ga in _repository.Games on st.GameID equals ga.ID
                         select new StatisticsViewModel
                         {
                             game = new Game { 
                                 Name = ga.Name
                             },
                             playah = new Player { 
                                 UserName = pl.UserName 
                             },
                             stats = new Statistic { 
                                 Win = st.Win, 
                                 Draw = st.Draw, 
                                 Loss = st.Loss 
                             }
                         });
            return model;
        }

        public void AddInstance(GameInstance model)
        {
            using (AppDataContext db = new AppDataContext())
            {
                db.GameInstances.Add(model);
                db.SaveChanges();
            }
        }

        public void AddStatistic(Statistic model)
        {
            using (AppDataContext db = new AppDataContext())
            {
                db.Statistics.Add(model);
                db.SaveChanges();
            }
        }

        public void AddGames(Game model)
        {
            using (AppDataContext db = new AppDataContext())
            {
                db.Games.Add(model);
                db.SaveChanges();
            }
        }
    }
}